package com.meishij;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meishij.adapter.MallFragmentAdapter;
import com.meishij.adapter.MallSellViewPagerAdapter;
import com.meishij.adapter.MallViewPagerAdapter;
import com.meishij.app.MyApp;
import com.meishij.entry.MallSellEntry;
import com.meishij.fragment.malldetailfragment.MallItemDetailFragment;
import com.meishij.fragment.malldetailfragment.MallMessageFragment;
import com.meishij.server.MallServer;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MallSellActivity extends AppCompatActivity implements Callback<MallSellEntry>, ViewPager.OnPageChangeListener, Handler.Callback {
    private final String source = "android";
    private final String format = "json";
    private ViewPager mall_sell_viewpager;
    private LinearLayout mall_sell_focus_points;
    private ImageView mall_sell_focus_point;
    private MallSellViewPagerAdapter mallSellViewPagerAdapter;
    private Handler handler = new Handler(this);
    private TextView mall_sell_mount;
    private TextView mall_sell_rest_message;
    private TextView mall_sell_title;
    private TextView mall_sell_descr;
    private TextView mall_sell_price;
    private TextView mall_sell_guige;
    private TextView mall_sell_market_price;
    private TextView mall_sell_postage;
    private TextView mall_sell_price_btn;
    private Toolbar mall_sell_toolbar;
    private TabLayout mall_sell_tablayout;
    private List<String> tabList;
    private ViewPager mall_sell_tab_viewpager;
    private List<Fragment> fragmentList=new ArrayList<>();
    private MallFragmentAdapter mallFragmentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall_sell);
        mall_sell_viewpager = (ViewPager) findViewById(R.id.mall_sell_viewpager);
        mall_sell_focus_points = (LinearLayout) findViewById(R.id.mall_sell_focus_points);
        mall_sell_focus_point = (ImageView) findViewById(R.id.mall_sell_focus_point);
        mall_sell_mount = (TextView) findViewById(R.id.mall_sell_mount);
        mall_sell_rest_message = (TextView) findViewById(R.id.mall_sell_rest_message);
        mall_sell_title = (TextView) findViewById(R.id.mall_sell_title);
        mall_sell_descr = (TextView) findViewById(R.id.mall_sell_descr);
        mall_sell_price = (TextView) findViewById(R.id.mall_sell_price);
        mall_sell_guige = (TextView) findViewById(R.id.mall_sell_guige);
        mall_sell_market_price = (TextView) findViewById(R.id.mall_sell_market_price);
        mall_sell_postage = (TextView) findViewById(R.id.mall_sell_postage);
        mall_sell_price_btn = (TextView) findViewById(R.id.mall_sell_price_btn);
        mall_sell_toolbar = (Toolbar) findViewById(R.id.mall_sell_toolbar);
        setSupportActionBar(mall_sell_toolbar);
        mall_sell_tablayout = (TabLayout) findViewById(R.id.mall_sell_tablayout);
        tabList=new ArrayList<>();
        tabList.add("商品详情");
        tabList.add("买家留言");
        mall_sell_tab_viewpager = (ViewPager) findViewById(R.id.mall_sell_tab_viewpager);
        Intent intent = getIntent();
        String i = intent.getStringExtra("i");
        String goodsSource = intent.getStringExtra("goodsSource");
        Log.e("自定义标签", "     " + i + "    " + goodsSource);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.meishi.cc")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<MallSellEntry> mallSell = retrofit.create(MallServer.class).getMallSell(source, format, null, null, i, goodsSource);
        mallSell.enqueue(this);
    }

    @Override
    public void onResponse(Call<MallSellEntry> call, Response<MallSellEntry> response) {
        MallSellEntry body = response.body();
        MallSellEntry.ObjBean obj = body.getObj();
        if (mall_sell_viewpager.getAdapter() == null) {
            List<MallSellEntry.ObjBean.ImagesBean> images = obj.getImages();
            for (MallSellEntry.ObjBean.ImagesBean image : images) {
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(R.drawable.mall_point_unchecked);
                int point_padding = (int) getResources().getDimension(R.dimen.point_padding);
                imageView.setPadding(point_padding, point_padding, point_padding, point_padding);
                mall_sell_focus_points.addView(imageView);
            }
            mallSellViewPagerAdapter = new MallSellViewPagerAdapter(this, images);
            mall_sell_viewpager.setAdapter(mallSellViewPagerAdapter);
            mall_sell_focus_point.post(new Runnable() {
                @Override
                public void run() {
                    mall_sell_focus_point.setVisibility(View.VISIBLE);
                    ViewCompat.setTranslationX(mall_sell_focus_point, mall_sell_focus_points.getLeft());
                }
            });
            mall_sell_viewpager.addOnPageChangeListener(this);
            mall_sell_viewpager.setCurrentItem(1);
            handler.obtainMessage(0, 1, 0).sendToTarget();
        }
        mall_sell_mount.setText("仅限" + obj.getTotal_store() + "份");
        int taken_mount = Integer.parseInt(obj.getTotal_store()) - obj.getStore();
        mall_sell_rest_message.setText(taken_mount + "份已被抢走,仅剩" + obj.getStore() + "份");
        mall_sell_title.setText(obj.getTitle());
        mall_sell_descr.setText(obj.getDescr());
        mall_sell_price.setText("¥"+obj.getPrice()+"/");
        mall_sell_guige.setText(obj.getGuige());
        mall_sell_market_price.setText(obj.getMarket_price());
        mall_sell_market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        mall_sell_postage.setText(obj.getPostage());
        mall_sell_price_btn.setText("¥"+obj.getPrice()+" 马上抢");
        Bundle bundle1 = new Bundle();
        bundle1.putSerializable("data",obj);
        MallItemDetailFragment mallItemDetailFragment = new MallItemDetailFragment();
        mallItemDetailFragment.setArguments(bundle1);
        MallMessageFragment mallMessageFragment = new MallMessageFragment();
        mallMessageFragment.setArguments(bundle1);
        fragmentList.add(mallItemDetailFragment);
        fragmentList.add(mallMessageFragment);
        mallFragmentAdapter = new MallFragmentAdapter(getSupportFragmentManager(),tabList,fragmentList);
        mall_sell_tab_viewpager.setAdapter(mallFragmentAdapter);
        mall_sell_tablayout.setupWithViewPager(mall_sell_tab_viewpager);
    }

    @Override
    public void onFailure(Call<MallSellEntry> call, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        float value = mall_sell_focus_points.getLeft() + (position - 1 + positionOffset) * mall_sell_focus_point.getWidth();
        value = Math.max(value, mall_sell_focus_points.getLeft());
        value = Math.min(value, mall_sell_focus_points.getRight() - mall_sell_focus_point.getWidth());
        ViewCompat.setTranslationX(mall_sell_focus_point, value);
    }

    @Override
    public void onPageSelected(int position) {
        PagerAdapter pagerAdapter = mall_sell_viewpager.getAdapter();
        if (position == 0) {
            mall_sell_viewpager.setCurrentItem(pagerAdapter.getCount() - 2, false);
        } else if (position == pagerAdapter.getCount() - 1) {
            mall_sell_viewpager.setCurrentItem(1, false);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {
            case 0:
                int arg1 = message.arg1;
                if (arg1 > mall_sell_viewpager.getAdapter().getCount() - 2) {
                    arg1 = 1;
                }
                mall_sell_viewpager.setCurrentItem(arg1, arg1 != 1);
                Message message1 = handler.obtainMessage(0, ++arg1, 0);
                handler.sendMessageDelayed(message1, 5000);
                break;
        }
        return true;
    }
}
